/**
 * 权限相关的工具类
 */
layui.define(function (exports) {
        var obj ={
            /**
             * 动态生成父代码选择框
             * @param tragetId 目标框id
             * @param permissionType 权限类型
             * @param flag 是否显示禁用的数据
             * @param selectOption 默认选中的数据
             * @param firstOption 第一个操作值
             */
            createParentCodeSelect:function (tragetId,permissionType,flag,selectOption,firstOption) {
                $.ajax({
                    url:'/back/permission/getList'
                    ,type:'post'
                    ,data:{
                        permissionType:permissionType,
                        permissionStatus:flag?"":"0"
                    }
                    ,async:false
                    ,dataType:'json'
                    ,success:function(res){
                        //动态生成父代码选择框
                        if(res.code == return_200){
                            var selectHtml = "";
                            selectHtml += "<option value=''>" + firstOption + "</option>";
                            $("#" + tragetId).empty();
                            for (var i = 0; i < res.data.length; i++) {
                                if(res.data[i].permissionStatus == '1'){
                                    selectHtml += "<option value=\"" + res.data[i].permissionCode + "\" disabled>" + res.data[i].permissionName + "</option>";
                                }else{
                                    if (selectOption && res.data[i].code == selectOption) {
                                        selectHtml += "<option value=\"" + res.data[i].permissionCode + "\" selected>" + res.data[i].permissionName + "</option>";
                                    } else {
                                        selectHtml += "<option value=\"" + res.data[i].permissionCode + "\">" + res.data[i].permissionName + "</option>";
                                    }
                                }
                            }
                            $("#" + tragetId).append(selectHtml);
                        }else{
                            console.log("初始化父代码选择框失败");
                        }
                    }
                    ,error:function(){
                        console.log("动态生成父代码选择框异常");
                    }
                });
            }
            /**
             * 授权页面显示权限
             * @param tragetId 目标id
             * @param flag 是否显示禁用数据
             * @param selectData 已选择的数据
             */
            ,createPremission:function (tragetId,flag,selectData) {
                $.ajax({
                    url:'/back/permission/getAuthorizeData'
                    ,type:'post'
                    ,data:{
                    }
                    ,async:false
                    ,dataType:'json'
                    ,success:function(res){
                        if(res.code == return_200){
                            $("#" + tragetId).empty();
                            for (var i = 0; i < res.data.length; i++) {
                                var fatherHtml = "";
                                if(selectData.indexOf(res.data[i].permissionCode) > -1){
                                    fatherHtml = "<tr>\n" +
                                        "                        <td>\n" +
                                        "                            <input type=\"checkbox\" checked value=\""+res.data[i].permissionCode+"\" name=\"permissionCode\" lay-skin=\"primary\" lay-filter=\"father\" title=\""+res.data[i].permissionName+"\">\n" +
                                        "                        </td>\n" +
                                        "                        <td>\n" +
                                        "                            <div class=\"layui-input-block\" id=\"F"+res.data[i].id+"\">\n" +
                                        "                            </div>\n" +
                                        "                        </td>\n" +
                                        "                    </tr>";
                                }else {
                                    fatherHtml = "<tr>\n" +
                                        "                        <td>\n" +
                                        "                            <input type=\"checkbox\" value=\""+res.data[i].permissionCode+"\" name=\"permissionCode\" lay-skin=\"primary\" lay-filter=\"father\" title=\""+res.data[i].permissionName+"\">\n" +
                                        "                        </td>\n" +
                                        "                        <td>\n" +
                                        "                            <div class=\"layui-input-block\" id=\"F"+res.data[i].id+"\">\n" +
                                        "                            </div>\n" +
                                        "                        </td>\n" +
                                        "                    </tr>";
                                }
                                $("#" + tragetId).append(fatherHtml);
                                for(var j = 0;j < res.data[i].childPermissons.length;j++){
                                    var childrenHtml = "";
                                    if(selectData.indexOf(res.data[i].childPermissons[j].permissionCode) > -1){
                                        childrenHtml = "<input name=\"permissionCode\" lay-skin=\"primary\" type=\"checkbox\" checked title=\""+res.data[i].childPermissons[j].permissionName+"\" value=\""+res.data[i].childPermissons[j].permissionCode+"\">";
                                    }else {
                                        childrenHtml = "<input name=\"permissionCode\" lay-skin=\"primary\" type=\"checkbox\" title=\""+res.data[i].childPermissons[j].permissionName+"\" value=\""+res.data[i].childPermissons[j].permissionCode+"\">";
                                    }
                                    $("#F" + res.data[i].id).append(childrenHtml);
                                }

                            }

                        }else{
                            console.log("初始化授权数据失败");
                        }
                    }
                    ,error:function(){
                        console.log("动态生成授权数据异常");
                    }
                });
            }
        };
        exports("permissionUtil",obj);
}
);